/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyStatementExecution;

import MyDatabaseHandler.DatabaseHandler;
import MyStatementExecution.MyDelete.MyDeleteStatement;
import MyStatementExecution.MyInsert.MyInsertStatement;
import MyStatementExecution.MySelect.MySelectStatement;
import MyStatementExecution.MyUpdate.MyUpdateStatement;
import MyDataContainer.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Welcome
 */
public class ExecutionHandler {
    
    //<editor-fold defaultstate="collapsed" desc="1/ Các thuộc tính cơ bản">
    
    private ArrayList<MyStatementKind> statementKinds;  //Danh sách các loai câu truy vấn hỗ trợ
    private DatabaseHandler databaseHandler;
    private MyQuery selectQuery;
    
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="2/ Các phương thức khởi tạo">
    public void addStatementKind()
    {
        statementKinds.add(MyStatementKind.SELECT);
        statementKinds.add(MyStatementKind.INSERT);
        statementKinds.add(MyStatementKind.UPDATE);
        statementKinds.add(MyStatementKind.DELETE);
    }
    public ExecutionHandler(DatabaseHandler dbHandler) {
        this.databaseHandler=dbHandler;
        this.statementKinds = new ArrayList<MyStatementKind>();
        addStatementKind();
       
    }
    public ExecutionHandler() {
        this.databaseHandler=new DatabaseHandler();
        this.statementKinds = new ArrayList<MyStatementKind>();
        addStatementKind();
        
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="3/Các phương pháp Getter/Setter">

    public MyQuery getSelectQuery() {
        return selectQuery;
    }

    
    public void setDatabaseHandler(DatabaseHandler databaseHandler) {
        this.databaseHandler = databaseHandler;
    }

    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="4/ Các phương thức xử lý nghiệp vụ">
    public MyStatement identifySqlStatement(String SQLStatement)
    {
        SQLStatement = SQLStatement.toLowerCase();
        MyStatement myStatement=null;    //Để xử lý các câu truy vấn
        MyStatementKind myStatementKind=null;
        for(int i=0;i<statementKinds.size();i++)
        {
            String tempKind = statementKinds.get(i).toString().toLowerCase();
            int pos = SQLStatement.indexOf(tempKind);
            if(pos >-1)
            {
                myStatementKind = statementKinds.get(i);
            }
        }
        
        switch(myStatementKind)
        {
            case INSERT:
                myStatement=new MyInsertStatement();
                break;
            case SELECT:
                myStatement=new MySelectStatement();
                break;
            case DELETE:
                myStatement=new MyDeleteStatement();
                break;
            case UPDATE:
                myStatement=new MyUpdateStatement();
                break;
            default:
                System.out.println("Kind of sql stament is not supported!");
                break;
        }
        
        return myStatement;
    }
    
    public void processExecution(String inputSQLString)
    {

        MyStatement sqlStatement= identifySqlStatement(inputSQLString);
        sqlStatement.setStatementString(inputSQLString);
        sqlStatement.setDatabaseHandler(databaseHandler);
        
        System.out.println(sqlStatement.getKind().toString());
        
        selectQuery=new MyQuery();
        if(sqlStatement.getKind()==MyStatementKind.SELECT)
        {
            selectQuery = ((MySelectStatement)sqlStatement).executeSelectStatement();
            //Xu ly tiep, gui ve client chang han
            
        }
        else
            sqlStatement.executeStatement();
    }
    //</editor-fold>

}